﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExportToPLMX.Managers.CApExportToPlmxHierarchyManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections.Generic;
using System.Linq;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ExportToPLMX.Managers;

public class CApExportToPlmxHierarchyManager(string folderPath) : CApExportToPlmxExportManager(folderPath + "\\init_data.js")
{
  internal const string m_exportFileName = "init_data.js";

  internal void ExportHierarchy(List<string> ids)
  {
    TxObjectsHierarchy.CreateFlatHierarchy(ids);
    this.CreateNodeHierarchyToExport().ToJSON(this.ExportPath);
    TxObjectsHierarchy.GetHierarchy().Clear();
  }

  private TxObjectNode CreateNodeHierarchyToExport()
  {
    KeyValuePair<string, TxObjData> keyValuePair = TxObjectsHierarchy.GetHierarchy().Where<KeyValuePair<string, TxObjData>>((Func<KeyValuePair<string, TxObjData>, bool>) (pair => pair.Value.level == 0)).First<KeyValuePair<string, TxObjData>>();
    TxObjectNode node = new TxObjectNode(keyValuePair.Value.Name, keyValuePair.Key);
    CApExportToPlmxHierarchyManager.SetNodeRelToParent(node);
    CApExportToPlmxHierarchyManager.SetNodeBoundingBox(node);
    node.children = CApExportToPlmxHierarchyManager.CreateChildrenNodes(keyValuePair.Value.Children);
    return node;
  }

  private static void SetNodeRelToParent(TxObjectNode node)
  {
    TxTransformation txTransformation = new TxTransformation();
    ITxObject objectById = TxApplication.ActiveDocument.GetObjectById(node.psId);
    if (!(objectById is ITxObjectRoot))
    {
      ITxLocatableObject itxLocatableObject = objectById as ITxLocatableObject;
      ITxLocatableObject collection = ((ITxObject) itxLocatableObject).Collection as ITxLocatableObject;
      txTransformation = itxLocatableObject.GetLocationRelativeToObject(collection).Transpose(txTransformation);
    }
    node.xform[0] = txTransformation[0, 0];
    node.xform[1] = txTransformation[0, 1];
    node.xform[2] = txTransformation[0, 2];
    node.xform[3] = txTransformation[1, 0];
    node.xform[4] = txTransformation[1, 1];
    node.xform[5] = txTransformation[1, 2];
    node.xform[6] = txTransformation[2, 0];
    node.xform[7] = txTransformation[2, 1];
    node.xform[8] = txTransformation[2, 2];
    node.xform[9] = txTransformation[3, 0];
    node.xform[10] = txTransformation[3, 1];
    node.xform[11] = txTransformation[3, 2];
  }

  private static void SetNodeBoundingBox(TxObjectNode node)
  {
    ITxObject objectById = TxApplication.ActiveDocument.GetObjectById(node.psId);
    ITxLocatableObject itxLocatableObject = objectById as ITxLocatableObject;
    TxBoundingBoxCalculator boundingBoxCalculator = new TxBoundingBoxCalculator();
    boundingBoxCalculator.AddObject(objectById);
    TxBoundingBox txBoundingBox;
    try
    {
      txBoundingBox = boundingBoxCalculator.CalculateVisibleMinimalBoundingBox();
    }
    catch (Exception ex)
    {
      txBoundingBox = new TxBoundingBox();
    }
    TxVector translation = itxLocatableObject.AbsoluteLocation.Translation;
    node.bboxMin[0] = txBoundingBox.Lower.X - translation.X;
    node.bboxMin[1] = txBoundingBox.Lower.Y - translation.Y;
    node.bboxMin[2] = txBoundingBox.Lower.Z - translation.Z;
    node.bboxMax[0] = txBoundingBox.Higher.X - translation.X;
    node.bboxMax[1] = txBoundingBox.Higher.Y - translation.Y;
    node.bboxMax[2] = txBoundingBox.Higher.Z - translation.Z;
  }

  private static List<TxObjectNode> CreateChildrenNodes(List<string> children)
  {
    List<TxObjectNode> childrenNodes = new List<TxObjectNode>();
    if (children != null && children.Any<string>())
    {
      foreach (string child in children)
      {
        TxObjData txObjData = TxObjectsHierarchy.GetHierarchy()[child];
        TxObjectNode node = new TxObjectNode(txObjData.Name, txObjData.Id);
        childrenNodes.Add(node);
        CApExportToPlmxHierarchyManager.SetNodeRelToParent(node);
        CApExportToPlmxHierarchyManager.SetNodeBoundingBox(node);
        node.children = CApExportToPlmxHierarchyManager.CreateChildrenNodes(txObjData.Children);
      }
    }
    return childrenNodes;
  }
}
